cd $firmpath

use new WUI_ uncertainty gr_gdp new pm trim ct cty rs_hs6_b pmt year id_conc iso2 export crisis die using base4reg_u19962010, clear
	g eta_uncert=uncertainty*rs
	g eta_WUI=WUI_*rs
	g eta_gdp=gr_gdp*rs
	g eta_crisis=crisis*rs
	

preserve
collapse (median) rs_hs6_b, by(id_conc)
forvalues i=10(10)90{
	egen RS`i'=pctile(rs), p(`i')
	sum RS`i'
	local p`i'=r(mean)
}
restore



g RS10=`p10'
g RS20=`p20'
g RS30=`p30'
g RS40=`p40'
g RS50=`p50'
g RS60=`p60'
g RS70=`p70'
g RS80=`p80'
g RS90=`p90'


grstyle init
grstyle set plain, horizontal grid dotted

ppmlhdfe die uncertainty  eta_uncert  gr_gdp eta_gdp if year < 2007, a(pmt cty)  tol(1.000e-06) 
estat vce
mat v = e(V)
local cov = v[1,2]
display "`cov'"
local beta1=_b[uncertainty]
local beta2=_b[eta_uncert]
local sdbeta1=_se[uncertainty]
local sdbeta2=_se[eta_uncert]

di "`beta1'"
di "`beta2'"
di "`sdbeta1'"
di "`sdbeta2'"

preserve
collapse (mean) RS*
g i=1
reshape long RS, i(i) j(quantile)
g coef=0
replace coef=(`beta1'+`beta2'*RS)
g sd=sqrt(`sdbeta1'*`sdbeta1'+RS^2*`sdbeta2'*`sdbeta2'+2*RS*`cov')
sum sd, d
g coef_inf=coef-1.96*sd
g coef_sup=coef+1.96*sd
tab coef 
gr tw (line coef quantile, lc(red)) (line coef_inf quantile, lpattern(dash) lc(gray)) (line coef_sup quantile,  lpattern(dash) lc(gray)), legend(off) xtitle("Percentiles of RS distribution") ytitle("Impact of an uncertainty shock")
gr export "$outputpath/figure2_death.pdf", as(pdf) replace
 restore
 
 
 ppmlhdfe new uncertainty eta_uncert gr_gdp eta_gdp  if year > 1999, a(pmt cty)  tol(1.000e-06)  vce(ro)
 estat vce
mat v = e(V)
local cov = v[1,2]
display "`cov'"
local beta1=_b[uncertainty]
local beta2=_b[eta_uncert]
local sdbeta1=_se[uncertainty]
local sdbeta2=_se[eta_uncert]

di "`beta1'"
di "`beta2'"
di "`sdbeta1'"
di "`sdbeta2'"

preserve
collapse (mean) RS*
g i=1
reshape long RS, i(i) j(quantile)
g coef=0
replace coef=(`beta1'+`beta2'*RS)
g sd=sqrt(`sdbeta1'*`sdbeta1'+RS^2*`sdbeta2'*`sdbeta2'+2*RS*`cov')
sum sd, d
g coef_inf=coef-1.96*sd
g coef_sup=coef+1.96*sd
tab coef 
gr tw (line coef quantile, lc(red)) (line coef_inf quantile, lpattern(dash) lc(gray)) (line coef_sup quantile,  lpattern(dash) lc(gray)), legend(off) xtitle("Percentiles of RS distribution") ytitle("Impact of an uncertainty shock")
gr export "$outputpath/figure2_entry.pdf", as(pdf) replace
 restore
 
